package com.example.zfbsdklibrary.zfb;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import com.alipay.share.sdk.openapi.APImageObject;
import com.alipay.share.sdk.openapi.APMediaMessage;
import com.alipay.share.sdk.openapi.APTextObject;
import com.alipay.share.sdk.openapi.APWebPageObject;
import com.alipay.share.sdk.openapi.SendMessageToZFB;
import com.example.zfbsdklibrary.callback.NoInstalled;
import com.example.zfbsdklibrary.utils.Tools;

import java.io.File;

/**
 * 作者：huyi
 * 日期：2020/12/17 10:12
 * 描述：
 */
public class ZfbShareUtil {

    private static final int THUMB_SIZE = 150;

    private ZfbShareUtil() {

    }

    public static ZfbShareUtil newInstance() {
        return new ZfbShareUtil();
    }

    private NoInstalled noInstalled;

    /**
     * 注册支付宝未安装监听
     */
    public ZfbShareUtil setNoInstalled(NoInstalled noInstalled) {
        this.noInstalled = noInstalled;
        return this;
    }

    private NoInstalled getNoInstalled() {
        if (null == noInstalled) {
            noInstalled = (transaction) -> {
            };
        }
        return noInstalled;
    }


    /**
     * 支付宝分享文本
     * @param msg 文本内容
     */
    public void zfbTxtShare(String msg) {
        //组装文本消息内容对象
        APTextObject textObject = new APTextObject();
        textObject.text = msg;

        //组装分享消息对象
        APMediaMessage mediaMessage = new APMediaMessage();
        mediaMessage.mediaObject = textObject;

        //将分享消息对象包装成请求对象
        SendMessageToZFB.Req req = new SendMessageToZFB.Req();
        req.message = mediaMessage;
        //发送请求
        ZfbApiGlobal.getInstance().getApi().sendReq(req);
    }

    /**
     * 支付宝bitmap图片分享
     *
     * @param context 上下文
     * @param bitmap   分享的bitmap
     */
    public void zfbBitmapShare(Context context, Bitmap bitmap) {
        if (zfbNoInstalled()) {
            return;
        }
        if (bitmap == null) {
            getNoInstalled().noInstalled("分享的图片为空");
            return;
        }
        APImageObject imageObject = new APImageObject(bitmap);
        APMediaMessage mediaMessage = new APMediaMessage();
        mediaMessage.mediaObject = imageObject;
        SendMessageToZFB.Req req = new SendMessageToZFB.Req();
        req.message = mediaMessage;
        req.transaction = buildTransaction("image");
        bitmap.recycle();
        ZfbApiGlobal.getInstance().getApi().sendReq(req);
    }

    /**
     *支付宝图片链接分享
     *
     * @param context 上下文
     * @param url   分享的图片链接
     */
    public void zfbOnlineImageShare(Context context, String url) {
        if (zfbNoInstalled()) {
            return;
        }
        if (TextUtils.isEmpty(url)) {
            getNoInstalled().noInstalled("分享的图片链接为空");
            return;
        }
        APImageObject imageObject = new APImageObject();
        imageObject.imageUrl = url;
        APMediaMessage mediaMessage = new APMediaMessage();
        mediaMessage.mediaObject = imageObject;
        SendMessageToZFB.Req req = new SendMessageToZFB.Req();
        req.message = mediaMessage;
        req.transaction = buildTransaction("image");
        ZfbApiGlobal.getInstance().getApi().sendReq(req);
    }

    /**
     * 支付宝文件类型的图片分享
     *
     * @param context 上下文
     * @param file     图片文件
     */
    public void zfbLocalImageShare(Context context, File file) {
        if (zfbNoInstalled()) {
            return;
        }
        if (file == null) {
            getNoInstalled().noInstalled("分享的图片为空");
            return;
        }
        APImageObject imageObject = new APImageObject();
        imageObject.imagePath = file.getAbsolutePath();
        APMediaMessage mediaMessage = new APMediaMessage();
        mediaMessage.mediaObject = imageObject;
        SendMessageToZFB.Req req = new SendMessageToZFB.Req();
        req.message = mediaMessage;
        req.transaction = buildTransaction("image");
        ZfbApiGlobal.getInstance().getApi().sendReq(req);
    }


    /**
     * 支付宝网页分享
     *
     * @param context  上下文
     * @param title     分享的标题
     * @param summary   分享的消息摘要
     * @param targetUrl 这条分享消息被好友点击后的跳转URL
     * @param resLogo   资源图片
     */
    public void zfbHttpShare(Context context, String title, String summary, String targetUrl, int resLogo) {
        if (zfbNoInstalled()) {
            return;
        }
        APWebPageObject webPageObject = new APWebPageObject();
        webPageObject.webpageUrl = targetUrl;
        APMediaMessage webMessage = new APMediaMessage();
        webMessage.title = title;
        webMessage.description = summary;
        webMessage.mediaObject = webPageObject;
        //设置缩略图
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resLogo);
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
        bmp.recycle();
        webMessage.thumbData = Tools.bmpToByteArray(thumbBmp, true);
        SendMessageToZFB.Req webReq = new SendMessageToZFB.Req();
        webReq.message = webMessage;
        webReq.transaction = buildTransaction("webpage");
        ZfbApiGlobal.getInstance().getApi().sendReq(webReq);
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }


    /**
     * 注册回调接口，并检查是否安装支付宝
     */
    private boolean zfbNoInstalled() {
        //检查是否安装微信客户端
        if (!ZfbApiGlobal.getInstance().getApi().isZFBAppInstalled()) {
            getNoInstalled().noInstalled("请安装支付宝客户端");
            return true;
        }
        return false;
    }
}
